package com.qdsg.modulesihealsdk.ecgSingle.tool;

import com.qdsg.modulesihealsdk.ecgSingle.callback.ECGSingleCallback;
import com.qdsg.modulesihealsdk.entity.ECGRecord;
import com.qdsg.modulesihealsdk.entity.EcgSingleEntity;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


/**
 * @author fangrf 2017/07/15
 */
public class MyUtil {
	
	/**
	 * @param filePath
	 * @param fileName
	 * @param datas
	 */
	public static void writeFile(String filePath,String fileName,List<Integer> datas,ECGRecord ecgRecord,int nResult,int nHR){
		if(datas==null || datas.size()==0){
			return;
		}
		
		File dir = new File(filePath);
        if (!dir.exists()) {
        	dir.mkdir(); //创建文件夹
        }
        
        filePath = filePath+"/"+fileName;
        
        File file = new File(filePath);
		if (file.exists()) {		 
			file.delete();
		}
		try {
			file.createNewFile();//在文件夹中创建文件
		} catch (IOException e) {
			e.printStackTrace();
		}
 		
		int length = datas.size();
		byte[] buffer = new byte[length*4+4];//4 byte for record 
		int index = 0;
		//1 int data for record
		if(ecgRecord!=null){
			buffer[index++]= (byte) ecgRecord.getResultCode();
			buffer[index++]= (byte) ecgRecord.getGain();
			buffer[index++]= (byte) (ecgRecord.getAvgHR() & 0xff);//avgHR low
			buffer[index++]= (byte) ((ecgRecord.getAvgHR() & 0xff00) >> 8);//avgHR high
			//Log.d("TAG", "write--> gain:"+ecgRecord.gain+",resultCode:"+ecgRecord.resultCode+",HR:"+ecgRecord.avgHR);	
		}
		
		for(int j = 0;j < length;j++){							
			int data = datas.remove(0);
			/* 保存1个int数据，低字节在前，高字节在后 */
			buffer[index++] =  (byte) (data & 0xff);
			buffer[index++] =  (byte) ((0xff00 & data) >> 8);
			buffer[index++] =  (byte) ((0xff0000 & data) >> 16);
			buffer[index++] =  (byte) ((0xff000000 & data) >> 24);			
		}
		//System.out.println("datas.size()*4="+datas.size()*4 +",index->"+index);
		
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(filePath,true);
			fos.write(buffer, 0, index);
			fos.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(fos!= null){
					fos.close();
					EcgSingleEntity EcgSingleEntity = new EcgSingleEntity(nResult,nHR);
					if (ECGSingleCallback.getInstance(null).iecgSingle != null){
						ECGSingleCallback.getInstance(null).iecgSingle.resultReceived(EcgSingleEntity);
					}
				}
				fos = null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	/**
	 * @param filePath
	 * @return
	 */
	public static List<Integer> readFile(String filePath, ECGRecord ecgRecord) {
		try {
			FileInputStream fin = new FileInputStream(filePath);
			InputStream in = new BufferedInputStream(fin);

			byte[] buffer = new byte[1024];
			int index = 0;
			int data = 0;
			List<Integer> datas = new ArrayList<Integer>();

			while ((index = in.read(buffer)) != -1) {
				for (int j = 0; j < index;) {//还原1个int数据
					data = buffer[j++] & 0xff;
					data = data + ((buffer[j++] << 8) & 0xff00);
					data = data + ((buffer[j++] << 16) & 0xff0000);
					data = data + ((buffer[j++] << 24) & 0xff000000);
					datas.add(data);
				}
			}
			in.close();
			if (datas.size() == 0){
				ECGSingleCallback.getInstance(null).iecgSingle.onError("datas.size() == 0");
			}
			//get first int data
			if(ecgRecord!=null && datas.size() > 0){
				int record = datas.remove(0);
				ecgRecord.setResultCode(record&0xff);
				ecgRecord.setGain((record>>8)&0xff);
				ecgRecord.setAvgHR((record>>16)&0xff+(record>>24)&0xff);
				//Log.i("TAG","read->"+ ecgRecord.toString());
			}
			
			return datas;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	

	
}
